import numpy as np
from 数学建模国赛算法准备.遗传算法.快递公司送货策略 import calculateMat

point_height = []

with open('height.txt', 'r', encoding='utf-8') as f:
    lines = f.readlines()  # 读取每一行数据
    # 处理每一行数据
    for line in lines:
        line = line.split('\n')[0]
        point_height.append(float(line))

sum_height = np.array(point_height)
print(sum_height.sum())

# 距离矩阵
_mat = calculateMat.calculate()

# 通过MSTP的matlab文件得到的结果
before_calculate_path = [
    2, 6, 4, 5, 16, 17, 18, 24, 25, 19, 14, 20, 7, 13, 12, 11, 15, 27, 26, 28, 29, 30, 23, 21, 22, 10, 9, 8, 3, 1
]
# before_calculate_path.reverse()
print(before_calculate_path)
before_calculate_path = np.array(before_calculate_path)
before_calculate_path = before_calculate_path
"""
# 经过matlab计算得到的中断点
break_point = [5,8,11,14,20,24,27]

"""
# 下面利用中断点划分八个路径
calculate_path = []
temp = []
temp_height = 0
height = []  # 每条路线的重量

for i in range(len(before_calculate_path)):
    if temp_height + point_height[before_calculate_path[i]] > 25:
        height.append(temp_height)
        calculate_path.append(temp)
        temp = []
        temp_height = 0
        temp.append(before_calculate_path[i])
        temp_height += point_height[before_calculate_path[i]]
    else:
        temp.append(before_calculate_path[i])
        temp_height += point_height[before_calculate_path[i]]
calculate_path.append(temp)
height.append(temp_height)

distance = []  # 各个路线的距离
time = []  # 各个路线的时间
point_num = []  # 每个路线点的数目
v = 25  # 速度




for k in range(len(calculate_path)):
    outset = 0
    temp_distance = 0
    path_point_num = len(calculate_path[k])
    temp_height = 0
    for j in range(path_point_num):
        temp_distance += _mat[outset, calculate_path[k][j]]
        outset = calculate_path[k][j]
    distance.append(temp_distance+_mat[0, outset])
    temp_time = 0.167*len(calculate_path[k])+(temp_distance+_mat[0, outset])/v
    time.append(temp_time)
    point_num.append(path_point_num)

print('时间')
print(time)
print('经过的站点')
print(point_num)
print('距离')
print(distance)
print('计算后的路径')
print(calculate_path)
print('路径携带货物重量')
print(height)

new_time = []  # 各个路线的时间

calculate_path = [
    [2, 6, 4, 5, 16],
    [17, 18, 24],
    [25, 19, 14],
    [20, 7, 13],
    [12, 11, 15],
    [27, 26],
    [28, 29, 30, 23],
    [9, 10, 22, 21, 8],
    [3, 1]
]

"""
calculate_path = [
    [2, 4, 5, 16, 6],
    [24, 18, 17],
    [19, 25, 14],
    [7, 13, 20],
    [12, 15, 11],
    [27, 26],
    [29, 28, 30, 23],
    [10, 22, 21, 8, 9],
    [1, 3]
]


"""


price = []
go_price = []
back_price = []
calculate_path[2].reverse()
calculate_path[6].reverse()
calculate_path[7].reverse()
calculate_path[8].reverse()
for k in range(len(calculate_path)):
    outset = 0
    temp_distance = 0
    path_point_num = len(calculate_path[k])
    go_height = height[k]
    temp_go_price = 0
    for j in range(path_point_num):
        current_distance = _mat[outset, calculate_path[k][j]]
        temp_distance += current_distance
        temp_go_price += current_distance*3*go_height
        go_height -= point_height[calculate_path[k][j]]
        outset = calculate_path[k][j]
    temp_new_time = 0.167 * len(calculate_path[k]) + temp_distance/20 + _mat[0, outset] / 30
    temp_back_price = 2*_mat[0, outset]
    temp_price = temp_go_price+temp_back_price
    price.append(temp_price)
    go_price.append(temp_go_price)
    back_price.append(temp_back_price)
    new_time.append(temp_new_time)

# 新的时间
print('新的时间')
print(new_time)

# 价格
print('价格')
print(price)

# 去程价格
print('去程价格')
print(go_price)

# 返程价格
print('返程价格')
print(back_price)